<template>
  <div>
    <Alert type="error" v-if="errMsg.show">{{errMsg.msg}}</Alert>
    <Row>
      <Col span="24">
        <div  id='CameraCtl' style="width:100%; height: 400px">  </div>
      </Col>
      <Col span="24">
      <Select v-model="fenbianlv" style="width:200px;margin-top: 5px;" @on-change="SelectResolution">
          <Option v-for="item in FenBianLvList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
      </Col>
      <Col span="24">
        <Button type="primary" class="camera_btn" @click="TakePhoto">拍照</Button>
        <Button type="primary" class="camera_btn" @click="Cam_Focus">对焦</Button>
        <Button type="primary" class="camera_btn" @click="Cam_ZoomIn">放大</Button>
        <Button type="primary" class="camera_btn" @click="Cam_ZoomOut">缩小</Button>
      </Col>
    </Row>
  </div>
</template>

<script>
import { upBase64 } from '@/api/common'
export default {
  name: 'height-camera',
  components: {},
  props: {
    isOpen: false
  },
  data () {
    return {
      rules: {},
      errMsg: {
        show: false,
        msg: ''
      },
      FenBianLvList: [
        { label: '选择', value: '1' }
      ],
      fenbianlv: ''
    }
  },
  created () {},
  watch: {
    isOpen (e) {
      if (e === true) {
        var obj = $('CameraCtl')
        Cam_ControlInit(obj, 0, 0, 400, 400, this.CallBackFun)
      } else {
        Cam_Close()
      }
    }
  },
  methods: {
    formatDate (time) {
      let date = new Date(time)
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      let hour = date.getHours()
      let min = date.getMinutes()
      let sec = date.getSeconds()
      let newTime = year +
        (month < 10 ? '0' + month : month) +
        (day < 10 ? '0' + day : day) +
        (hour < 10 ? '0' + hour : hour) +
        (min < 10 ? '0' + min : min) +
        (sec < 10 ? '0' + sec : sec)
      return newTime
    },
    ShowInfo (op) {
      this.errMsg = { show: true, msg: op }
      console.log('报错信息', op)
      let that = this
      setTimeout(function () {
        that.errMsg = { show: false, msg: '' }
        console.log('gub')
      }, 2000)
    },
    CallBackFun (state, param) {
      if (state === 'GetDevCountAndNameResultCB') {
        // (必须重写该函数）返回获取的设备数目及设备名称
        this.GetDevCountAndNameResultCB(param[0], param[1])
      }
      if (state === 'GetResolutionResultCB') {
        // 返回获取的设备分辨率信息
        this.GetResolutionResultCB(param[0], param[1])
      }
      if (state === 'GetCaptrueImgResultCB') {
        // 返回获取的设备分辨率信息
        this.GetCaptrueImgResultCB(param[0], param[1], param[2])
      }
    },
    GetDevCountAndNameResultCB (devCount, devNameArr) {
      let jsonData = []
      if (devCount > 0) {
        for (var i = 0; i < devCount; i++) {
          jsonData.push({ 'lable': devNameArr[i], value: i })
        }
        // 获取分辨率
        console.log('准备获取分辨率')
        Cam_GetDevResolution(0)
      } else {
        this.ShowInfo('没有发现合适的设备！')
      }
    },
    GetResolutionResultCB (resCount, resArr) {
      if (resCount > 0) {
        let selectLabel = '2592*1944'
        let selectIndex = 0
        let fenBianLv = []
        for (var i = 0; i < resCount; i++) {
          let temp = { value: i, label: resArr[i] }
          fenBianLv.push(temp)
          // 默认500万分辨率打开
          if (resArr[i] === '2592*1944') {
            selectIndex = i
          }
        }
        this.FenBianLvList = fenBianLv
        // 打开摄像头
        Cam_Close()
        console.log(fenBianLv, '分辨率的数据')
        var restr = selectLabel
        var pos = restr.lastIndexOf('*')
        var width = parseInt(restr.substring(0, pos))
        var height = parseInt(restr.substring(pos + 1, restr.length))
        console.log(selectIndex, width, height, '传递过去的参数')
        Cam_Open(0, width, height)
      } else {
        this.ShowInfo('获取分辨率信息失败!')
      }
    },
    GetCaptrueImgResultCB (flag, path, base64Str) {
      if (flag === 0) {
        console.log(base64Str)
        this.upimg(base64Str)
        this.ShowInfo('拍照成功，图片保存位置：')
      } else {
        this.ShowInfo('拍照失败!')
      }
    },
    toOpenCamera () {
      this.ShowInfo('打开了设备！')
    },
    // 拍照
    TakePhoto () {
      let name = this.formatDate(new Date().getTime())
      let path = 'D:\\' + name + '.jpg'
      console.log('开始拍照啦', path)
      Cam_Photo(path) // 主摄像头拍照
    },
    // 对焦
    Cam_Focus () {
      Cam_Focus()
    },
    // 放大
    Cam_ZoomIn () {
      Cam_ZoomIn()
    },
    // 缩小
    Cam_ZoomOut () {
      Cam_ZoomOut()
    },
    // 选择分辨率
    SelectResolution (e) {
      console.log(e, 返回分辨率的值)
    },
    upimg (base64img) {
      let that = this
      upBase64(base64img).then(data => {
        that.$emit('ret-fun', data.data)
      })
    }
  }
}
</script>
<style>
  .camera_btn{
    margin-right: 5px;
    margin-top: 5px;
  }
</style>
